package com.xmad.ksadutils.AdSDK.AdMold.OnCallback;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.MainThread;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTSplashAd;

import com.xmad.ksadutils.AdSDK.Interface.AdOnLoad;
import com.xmad.ksadutils.AdSDK.Utlis.RxLogUtils;

public class PGLoadSplashScreenAd implements AdOnLoad {

    @Override
    public void noCallback(Long id, Activity activity, FrameLayout frameLayout) {

    }
    @Override
    public void noCallback(String id, Activity activity, FrameLayout frameLayout) {
        TTAdNative mTTAdNative = TTAdSdk.getAdManager().createAdNative(activity);
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(id)
                .setImageAcceptedSize(1080, 1920)
                .build();
        mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
            //请求广告失败
            @Override
            @MainThread
            public void onError(int code, String message) {
                Log.e("onError",code+message);
            }

            //请求广告超时
            @Override
            @MainThread
            public void onTimeout() {


            }

            //请求广告成功
            @Override
            @MainThread
            public void onSplashAdLoad(TTSplashAd ad) {
                if (ad == null) {
                    return;
                }
                //获取SplashView
                View view = ad.getSplashView();
                ad.startClickEye();
                if (view != null && frameLayout != null && !activity.isFinishing()) {
                    frameLayout.removeAllViews();
                    //把SplashView 添加到ViewGroup中,注意开屏广告view：width =屏幕宽；height >=75%屏幕高
                    frameLayout.addView(view);
                    //设置不开启开屏广告倒计时功能以及不显示跳过按钮,如果这么设置，您需要自定义倒计时逻辑
                    //ad.setNotAllowSdkCountdown();
                }else {

                }
                ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                    //点击回调
                    @Override
                    public void onAdClicked(View view, int type) {
                        RxLogUtils.e("onAdClicked");

                    }
                    //展示回调
                    @Override
                    public void onAdShow(View view, int type) {
                        RxLogUtils.e("onAdShow");
                        frameLayout.removeAllViews();
                    }
                    //跳过回调
                    @Override
                    public void onAdSkip() {
                        RxLogUtils.e("onAdSkip");


                    }

                    //超时倒计时结束
                    @Override
                    public void onAdTimeOver() {
                        RxLogUtils.e("onAdTimeOver");

                    }
                });
            }
        }, 5000);
    }
}
